package com.yjkf.zh.admin.controller;

import com.yjkf.zh.admin.aspect.LogAnnotation;
import com.yjkf.zh.admin.dao.SysRoleMapper;
import com.yjkf.zh.admin.model.SysRole;
import com.yjkf.zh.admin.model.SysRoleMenu;
import com.yjkf.zh.admin.service.SysRoleService;
import com.yjkf.zh.admin.util.SysConstant;
import com.yjkf.zh.core.http.HttpResult;
import com.yjkf.zh.core.page.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @PreAuthorize("hasAuthority('sys:role:view')")
    @PostMapping(value = "/findPage")
 //   @LogAnnotation(content = "查询角色列表")
    public HttpResult findPage(@RequestBody PageRequest pageRequest,HttpServletRequest request){
        Long userId = (Long)request.getSession().getAttribute("userId");
       /* boolean isEm= (pageRequest.getParams()).isEmpty();
        Map<Object, Object> params = new HashMap<>();
        if(!isEm){
            String roleName=pageRequest.getParams().get("roleName").toString();
            params.put("id",1L);
            params.put("roleName",roleName);
            pageRequest.setParams(params);
        }else {
            params.put("id", 1L);
            pageRequest.setParams(params);
        }*/
        return HttpResult.ok(sysRoleService.findPageBy(pageRequest,userId));
    }

    /**
     * 增加或修改
     * @param sysRole
     * @return
     */
    @PreAuthorize("hasAuthority('sys:role:add') AND hasAuthority('sys:role:edit')")
    @PostMapping(value="/iou")
    @LogAnnotation(content = "更新或修改角色操作")
    public HttpResult iou(@RequestBody SysRole sysRole,HttpServletRequest request){
        Long id = (Long)request.getSession().getAttribute("userId");
        SysRole sysRole1 = sysRoleService.findRoleById(sysRole.getId());
        if(sysRole1 != null){
            if(SysConstant.ADMIN.equalsIgnoreCase(sysRole1.getRoleName())){
                return HttpResult.error("超级管理员不允许修改!");
            }
        }
        //新增角色
        if((sysRole.getId() == null || sysRole.getId() ==0) && sysRoleService.findRoleNameById(sysRole.getRoleName(),id)!=null) {
            return HttpResult.error("角色名已存在!");
        }
        return HttpResult.ok(sysRoleService.save(sysRole));
    }

    //删除角色
    @PreAuthorize("hasAuthority('sys:role:delete')")
    @PostMapping(value="/deleteRole")
    @LogAnnotation(content = "删除角色操作")
    public HttpResult delete(@RequestBody List<SysRole> sysRole){

        return HttpResult.ok(sysRoleService.delete(sysRole));
    }

    @PreAuthorize("hasAuthority('sys:role:view')")
    @GetMapping(value = "/findRoleMenus")
    public HttpResult findRoleMenus(@RequestParam Long roleId){
        return HttpResult.ok(sysRoleService.findRoleMenus(roleId));
    }

    @PreAuthorize("hasAuthority('sys:role:edit')")
    @PostMapping(value = "/saveRoleMenus")
    @LogAnnotation(content = "修改角色权限操作")
    public HttpResult saveRoleMenu(@RequestBody List<SysRoleMenu> sysRoleMenus){
        for(SysRoleMenu sysRoleMenu:sysRoleMenus){
            SysRole sysRole = sysRoleMapper.findRoleById(sysRoleMenu.getRoleId());
            if(SysConstant.ADMIN.equalsIgnoreCase(sysRole.getRoleName())){
                //如果是超级管理员，不允许修改
                return HttpResult.error("超级管理员拥有所有菜单权限，不允许修改！");
            }
        }
        return HttpResult.ok(sysRoleService.saveRoleMenu(sysRoleMenus));
    }

   // @PreAuthorize("hasAuthority('sys:role:view')")
    @PostMapping(value = "/findSelfAll")
    public HttpResult findSelfAll(HttpServletRequest request){
        Long userId = (Long)request.getSession().getAttribute("userId");
        if(userId == 1) {
            return HttpResult.ok(sysRoleService.findAll());
        }else{
            return HttpResult.ok(sysRoleService.findSelfAll(userId));
        }
    }
}
